﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Web.Script.Serialization;

namespace Cstudy.MODEL
{
    public class InitModel<T> where T:new()
    {
        public static T Parse(DataRow dr)
        {
            T t = new T();
            Type infotype = typeof(T);
            PropertyInfo[] propertys = infotype.GetProperties();

            foreach (PropertyInfo pi in propertys)
            {
                infotype.GetProperty(pi.Name).SetValue(t, dr[pi.Name], null);
            }
            return t;
        }

        public static T Parse(DataSet ds)
        {
            return Parse(ds.Tables[0].Rows[0]);
        }

        public static string ToJson(T t)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(t);
        }
    }
}
